package ru.ccfit.nsu.g7204.dronov.adPlatform;

import ru.ccfit.nsu.g7204.dronov.adPlatform.system.db.DBConnectionHolder;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Drongo
 * Date: 21.03.11
 * Time: 20:48
 * To change this template use File | Settings | File Templates.
 */
public class ClientDetailsDBManager {
    private PreparedStatement getInfo1;
    private PreparedStatement getInfo2;
    private PreparedStatement getInfoStm;
    private ImplementationType type;


    public ClientDetailsDBManager(DBConnectionHolder dbConnectionHolder) {
        Connection conn = dbConnectionHolder.getConnection();
        try {
            getInfo1 =conn.prepareStatement("select * from complete_users1 where user=?");
            getInfo2 =conn.prepareStatement("select * from complete_users2 where user=?");
            getInfoStm = getInfo1;
        } catch (SQLException e) {
            ru.ccfit.nsu.g7204.dronov.adPlatform.utils.LoggingUtils.error(e.getLocalizedMessage(),e);
        }
    }

    public void setType(ImplementationType type) {
        if (!type.equals(this.type))
        {
            if (type.equals(ImplementationType.One))
            {
                getInfoStm = getInfo1;

            }
            else
            {
                getInfoStm = getInfo2;
            }
            this.type=type;
        }
    }

    public Client getClientInfo(String id)
    {
        Client c=null;
        try {
            getInfoStm.setString(1,id);
            List<ClientFeatureInfo> features=new ArrayList<ClientFeatureInfo>();
            ResultSet rs=getInfoStm.executeQuery();

            for (;rs.next();)
            {
                features.add(new ClientFeatureInfo(rs.getString("f_info"),rs.getDouble("shows"),rs.getDouble("clicks")));
            }
            if (features.size()>0)
                c=new Client(id,features);
        } catch (SQLException e) {
            ru.ccfit.nsu.g7204.dronov.adPlatform.utils.LoggingUtils.error(e.getLocalizedMessage(),e);
        }
        return c;
    }
}
